package com.cxh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_assetLv;
import com.common.entity.A_customer;
import com.common.entity.A_evaluate;
import com.common.entity.A_goods;
import com.common.entity.A_goodsType;
import com.common.utils.StringUtils;
import com.cxh.dao.A_goodsDao;
import com.cxh.dao.A_goodsTypeDao;
import com.cxh.dao.impl.A_goodsDaoImpl;
import com.cxh.dao.impl.A_goodsTypeDaoImpl;
import com.lyk.dao.A_assetLvDao;
import com.lyk.dao.A_customerDao;
import com.lyk.dao.impl.A_assetLvDaoImpl;
import com.lyk.dao.impl.A_customerDaoImpl;

public class A_goodsServlet extends HttpServlet {
	A_goodsDao gd = null;
	A_goodsTypeDao gtd = null;
	A_customerDao cd = null;
	A_assetLvDao ad = null;
	StringUtils su = su = new StringUtils();
	
	public void init() throws ServletException {
		gd = new A_goodsDaoImpl();
		gtd = new A_goodsTypeDaoImpl();
		cd = new A_customerDaoImpl();
		ad = new A_assetLvDaoImpl();
		
	};
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String opr = req.getParameter("opr");
		if("add".equals(opr)){
			Map<String, Object> mp = new HashMap<String, Object>();
			mp.put("goods_name",req.getParameter("goods_name"));
			mp.put("goodsType_id",su.str2Integer(req.getParameter("goodsType_id"), 0));
			mp.put("describe",req.getParameter("describe"));
			mp.put("buyoutPrice",su.str2Double(req.getParameter("buyoutPrice"), 1000.0));
			mp.put("minPrice",su.str2Double(req.getParameter("minPrice"), 100.0));
			mp.put("customer_id", su.str2Integer(req.getParameter("customer_id"), 1));
			mp.put("picImg",req.getParameter("picImg"));
			mp.put("examine",0);
			mp.put("assetLv_id",su.str2Integer(req.getParameter("assetLv_id"), 1));
			if(mp.get("goods_name")==null)
				System.out.println("*****************************************************************");
			int rows = gd.addA_goods(mp);
			PrintWriter out = resp.getWriter();
			List<A_goodsType> lgt = gtd.findByCondition(null, true);
			req.getSession().setAttribute("list_gt", lgt);
			List<A_customer> lc = cd.findByCondition(null);
			req.getSession().setAttribute("lc", lc);
			List<A_goods> lg = gd.findByCondition(null);
			req.getSession().setAttribute("lg", lg);
			List<A_assetLv> la = ad.findByCondition(null);
			req.getSession().setAttribute("la", la);
			if(rows>0){
				out.println("<script type='text/javascript'>alert('添加完成');location.href='GoodsCenter/goods_list_update.jsp';</script>");
			}
			else
				out.println("<script type='text/javascript'>alert('添加失败');location.href='GoodsCenter/goods_list_update.jsp';</script>");
		}else if("ztree".equals(opr)){
			List<A_customer> lc = cd.findByCondition(null);
			req.setAttribute("lc", lc);
			List<A_goodsType> lgt =  gtd.findByCondition(null, true);
			req.setAttribute("list_gt", lgt);
			List<A_assetLv> la = ad.findByCondition(null);
			req.setAttribute("la", la);
			
			req.getRequestDispatcher("GoodsCenter/goods_list_update.jsp").forward(req, resp);
		}else if("vagues".equals(opr)){
			Map<String, Object> mp = new HashMap<String, Object>();
			mp.put("goods_name",req.getParameter("search_name"));
			List<A_goods> lg = gd.findByCondition(mp);
			req.setAttribute("lg", lg);
			List<A_goodsType> lgt =  gtd.findByCondition(null, true);
			req.setAttribute("list_gt", lgt);
			req.getRequestDispatcher("GoodsCenter/goods_list.jsp").forward(req, resp);
		}else if("delsome".equals(opr)){
			int row = 0;
			int rows = 0;
			String errmsg="";
			String del_list = req.getParameter("del_list");
			String[] g_ids = del_list.split("|");
			for(String gid:g_ids){
				int id = 0;
				try{	
					id = su.str2Integer(gid, null);
					row = gd.deleteA_goods(id);
				}catch(Exception e){
					errmsg = e.getMessage();
				}
				rows+=row;
			}
			List<A_goods> lg = gd.findByCondition(null);
			PrintWriter out = resp.getWriter();
			req.setAttribute("lg", lg);
			List<A_goodsType> lgt =  gtd.findByCondition(null, true);
			req.setAttribute("list_gt", lgt);
			List<A_customer> lc = cd.findByCondition(null);
			req.setAttribute("lc", lc);
			List<A_assetLv> la = ad.findByCondition(null);
			req.setAttribute("la", la);
			if(rows>0)
				if(rows-g_ids.length==0)
					out.println("<script type='text/javascript'>alert('共"+g_ids.length+"条记录删除完成');</script>");
				else
					out.println("<script type='text/javascript'>alert('共"+rows+"条记录删除完成，"+(g_ids.length-rows)+"条记录删除失败，原因:+"+errmsg+"');</script>");
			else
				out.println("<script type='text/javascript'>alert('全部删除失败，原因:+"+errmsg+"');</script>");
		}else if("del".equals(opr)){
			int goods_id = su.str2Integer(req.getParameter("goods_id"), 0);
			int rows = gd.deleteA_goods(goods_id);
			PrintWriter out = resp.getWriter();
			if(rows>0)
				out.println("<script type='text/javascript'>location.href='goodsServlet';</script>");
			else
				out.println("<script type='text/javascript'>alert('删除失败，原因:+"+0+"');location.href='goodsServlet';</script>");
		}
		else{
			if(req.getSession().getAttribute("lc")!=null)
				req.getSession().removeAttribute("lc");
			if(req.getSession().getAttribute("list_gt")!=null)
				req.getSession().removeAttribute("list_gt");
			if(req.getSession().getAttribute("la")!=null)
				req.getSession().removeAttribute("la");
			if(req.getSession().getAttribute("lg")!=null)
				req.getSession().removeAttribute("lg");
			List<A_goodsType> lgt =  gtd.findByCondition(null, true);
			req.setAttribute("list_gt", lgt);
			List<A_customer> lc = cd.findByCondition(null);
			req.setAttribute("lc", lc);
			List<A_goods> lg = gd.findByCondition(null);
			req.setAttribute("lg", lg);
			List<A_assetLv> la = ad.findByCondition(null);
			req.setAttribute("la", la);
			req.getRequestDispatcher("GoodsCenter/goods_list.jsp").forward(req, resp);
		}
	}
	public void treee2zTree(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		List<A_goodsType> lgt =  gtd.findByCondition(null, true);
		StringBuffer tag = new StringBuffer("[{ id:0, pId:0, name:'商品类型列表', open:true},");
		for(A_goodsType gt:lgt){
			String item = "{ id:"+gt.getGoodsType_id()+", pId:"+gt.getSuper_type_id()+", name:'"+gt.getName()+" '},";
			tag.append(item);
		}
		String tag2 = tag.toString().substring(0, tag.length()-2);
		String zTree = new StringBuffer(tag2).append("]").toString();
		req.setAttribute("zTree", zTree);	
		req.getRequestDispatcher("GoodsCenter/goods_list_update.jsp").forward(req, resp);
	}
	@Override
	public void destroy() {
		super.destroy();
	}
}
